package com.carpool.finder;

import java.util.ArrayList;
import java.util.List;
 
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
 
import android.content.Context;
import android.util.Log;
 
public class UserFunctions {
 
    private JSONParser jsonParser;
 
    // Testing in localhost using wamp or xampp
    // use http://10.0.2.2/ to connect to your localhost ie http://localhost/
    private static String ur= "http://10.0.2.2";
    private static String loginURL = ur+"/carpooltest/login.php";
    private static String routeURL = ur+"/carpooltest/register.php";
    private static String showURL = ur+"/carpooltest/showroute.php";
    private static String deleteURL = ur+"/carpooltest/delete.php";
 
    private static String login_tag = "login";
    private static String route_tag = "register";
 
    // constructor
    public UserFunctions(){
        
    }
 
    /**
     * function make Login Request
     * @param email
     * @param password
     * */
    public JSONObject loginUser(String email, String password){
        // Building Parameters
    	jsonParser = new JSONParser();
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", login_tag));
        params.add(new BasicNameValuePair("email", email));
        params.add(new BasicNameValuePair("password", password));
        JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
        // return json
        return json;
    }
 
    /**
     * function Route registration
     * @param name
     * @param email
     * @param password
     * */
    
    public JSONObject registerRoute(String uid,String src_lat, String src_lon, String src_name, String des_lat, String des_lon, String des_name, String via_lat, String via_lon, String via_name){
        // Building Parameters
    	jsonParser = new JSONParser();
    	Log.e("Tag",route_tag);
    	//Log.e("Tag", ""+src_name);
    	//Log.e("Tag", ""+des_name);
    	Log.e("UID2", uid);
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "register"));
        params.add(new BasicNameValuePair("uid", uid));
        params.add(new BasicNameValuePair("s_lat", src_lat));
        params.add(new BasicNameValuePair("s_lon", src_lon));
        params.add(new BasicNameValuePair("s_name", src_name));
        params.add(new BasicNameValuePair("d_lat", des_lat));
        params.add(new BasicNameValuePair("d_lon", des_lon));
        params.add(new BasicNameValuePair("d_name", des_name));
        params.add(new BasicNameValuePair("v_lat", via_lat));
        params.add(new BasicNameValuePair("v_lon", via_lon));
        params.add(new BasicNameValuePair("v_name", via_name));
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(routeURL, params);
        // return json
        Log.e("route", ""+json);
        return json;
    }
    

    public JSONObject getRoute(String uid){
        // Building Parameters
    	jsonParser = new JSONParser();
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "show"));
        params.add(new BasicNameValuePair("uid", uid));
        JSONObject json = jsonParser.getJSONFromUrl(showURL, params);
        // return json
        return json;
    }
    
    public JSONObject deleteRoute(String uid){
        // Building Parameters
    	jsonParser = new JSONParser();
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "delete"));
        params.add(new BasicNameValuePair("uid", uid));
        JSONObject json = jsonParser.getJSONFromUrl(deleteURL, params);
        // return json
        return json;
    }
    /**
     * Function get Login status
     * */
    public boolean isUserLoggedIn(Context context){
        DatabaseHandler db = new DatabaseHandler(context);
        int count = db.getRowCount();
        if(count > 0){
            // user logged in
            return true;
        }
        return false;
    }
 
    /**
     * Function to logout user
     * Reset Database
     * */
    public boolean logoutUser(Context context){
        DatabaseHandler db = new DatabaseHandler(context);
        db.resetTables();
        return true;
    }
 
}